﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics;

namespace SnmpForm.SnmpWrapper
{
    public class SNMPHelper
    {
        private Process _process = null;
        private ProcessStartInfo _processInfo = null;

        private string _snmpgetFileName = null;
        private string _snmpsetFileName = null;
  
  //      private StreamReader sr = Process.StandardError;
   //     private StreamReader so = Process.StandardError
        public SNMPHelper()
        {
            _process = new Process();
            _processInfo = new ProcessStartInfo();

            _processInfo.UseShellExecute = false;
            _processInfo.CreateNoWindow = true;

            _processInfo.RedirectStandardOutput = true;
            _processInfo.RedirectStandardInput = true;
            _processInfo.RedirectStandardError = true;

            _process.StartInfo = _processInfo;
        }

        ~SNMPHelper()
        {
            _process.Close();
            _process.Dispose();
        }

        public bool InitSNMP(string get,string set)
        {
            _snmpgetFileName = get;
            _snmpsetFileName = set;
            return true;
        }

        public string SNMPget(string arg)
        {
            string strRet = "";
            _processInfo.FileName = _snmpgetFileName;
            _processInfo.Arguments = arg;
            _process.Start();

   /*         if (!_process.StandardError.EndOfStream)
            {
                strErrRet = _process.StandardError.ReadToEnd();
                Console.Write(strErrRet);
            }
   */      
            while (!_process.StandardOutput.EndOfStream)
            {
                String s = _process.StandardOutput.ReadLine();
                if (s != "")
                {
                    strRet += s;
                }
            }
            
 //           _process.Close();
            return strRet;
        }
        public bool SNMPset(string arg)
        {
   //         string strErrRet = null;
   //         string strOutRet = null;
            bool bRet = false;
            _processInfo.FileName = _snmpsetFileName;
            _processInfo.Arguments = arg;
            _process.Start();

 /*           if (!_process.StandardError.EndOfStream)
            {
                strErrRet = _process.StandardError.ReadToEnd();
                Console.Write(strErrRet);
            }
  */
            if (!_process.StandardOutput.EndOfStream)
            {
                bRet = true;
   /*             strOutRet = _process.StandardError.ReadToEnd();
                Console.Write(strOutRet);
    */
            }

            return bRet;
        }
    }
}
